package com.cs.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.cs.entity.Result;
import com.cs.mapper.UserMapper;
import com.cs.pojo.User;
import com.cs.pojo.UserExample;
import com.cs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class UserserviceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据id获取用户实体
     *
     * @param id
     * @return
     */
    @Override
    public User findUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    /***
     * @description 用户登录方法
     *
     * @param user
     * @date 2019/6/20 21:58
     * @return com.cs.entity.Result
     */
    @Override
    public Result login(User user) {
        Result result = null;
        if (!StringUtils.isEmpty(user.getUsername())) {
            UserExample userExample = new UserExample();
            userExample.createCriteria().andUsernameEqualTo(user.getUsername());
            List<User> users = userMapper.selectByExample(userExample);
            if (users != null && users.size() != 0) {
                if (user.getPassword().equals(users.get(0).getPassword())) {
                    result = new Result(true, "登录成功", users.get(0));
                }
            } else {
                result = new Result(false, "用户名或密码错误");
            }

        } else {
            result = new Result(false, "用户名不能为空");
        }
        return result;
    }

    /***
     * @description 用户注册
     *
     * @param user
     * @date 2019/6/20 21:42
     * @return com.cs.entity.Result
     */
    @Override
    public Result regist(User user) {
        Result result = null;
        if (!StringUtils.isEmpty(user.getUsername()) && !StringUtils.isEmpty(user.getPassword())) {
            //判断是否重名
            UserExample userExample = new UserExample();
            userExample.createCriteria().andUsernameEqualTo(user.getUsername());
            List<User> users = userMapper.selectByExample(userExample);
            if (users == null || users.size() == 0) {
                //表示用户名不存在
                user.setIdentity(2);
                userMapper.insert(user);
                result = new Result(true, "注册成功", user);
            } else {
                result = new Result(false, "用户名已存在");
            }
        } else {
            result = new Result(false, "用户名或密码不能为空");
        }
        return result;
    }

    /***
     * @description 修改用户信息
     *
     * @param user
     * @date 2019/6/20 21:44
     * @return com.cs.entity.Result
     */
    @Override
    public Result update(User user) {
        Result result = null;
        if (user.getId() != null) {
            userMapper.updateByPrimaryKeySelective(user);
            result = new Result(true, "修改成功", user);
        }
        return result;
    }

    /***
     * @description 获得所有学生列表
     *
     * @param
     * @date 2019/6/20 21:45
     * @return java.util.List<com.cs.pojo.User>
     */
    @Override
    public List<User> getStudents() {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andIdentityEqualTo(2);
        return userMapper.selectByExample(userExample);
    }

    /***
     * @description 设置为老师权限
     *
     * @param id
     * @date 2019/6/20 21:47
     * @return com.cs.entity.Result
     */
    @Override
    public Result updateIdentity(Integer id) {
        Result result = null;
        User user = userMapper.selectByPrimaryKey(id);
        if (user != null) {
            user.setIdentity(1);
            userMapper.updateByPrimaryKey(user);
            result = new Result(true, "修改成功");
        }
        return result;
    }

    @Override
    public List<User> findUserFuzzy(User user) {
        List<User> users = null;
        if (user != null) {
            UserExample userExample = new UserExample();
            userExample.createCriteria().andUsernameLike("%" + user.getUsername() + "%").andIdentityEqualTo(2);
            users = userMapper.selectByExample(userExample);
        }
        return users;
    }
}
